<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row

          type="flex"

          justify="space-between"

          align="middle"

          style="height: 40px"
        >
          <el-col :span="20">
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">{{ company.manager }}</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown trigger="click">
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree

          :data="departs"

          :props="defaultProps"
        >
          <template #default="{data}">
            <el-row

              type="flex"

              justify="space-between"

              align="middle"

              style="height: 40px;width:100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <el-dropdown trigger="click" @command="handleClick($event,data)">
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                        <el-dropdown-item command="edit">修改部门</el-dropdown-item>
                        <!-- @click.native="delDepts" -->
                        <el-dropdown-item command="del">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <addDepts
        ref="addDepts"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        :depart-list="departList"
        @add-success="getDepartmentList"
      />
    </div>

  </div>

</template>

<script>
import { getDepartmentsListApi, delDepartmentsApi } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
import AddDepts from './components/add-dept' // 引入新增部门组件
// import TreeTools from './components/tree-tools'

export default {
  name: 'Departments',
  components: {
    // TreeTools,
    AddDepts
  },
  data() {
    return {
      showDialog: false,
      nodeData: {},
      departList: [],
      isLoading: false,
      // list: [
      //   { part: '人事部', child: [{ part: '人事A' }, { part: '人事B' }] },
      //   {
      //     part: '财务部',
      //     child: [
      //       { part: '财务核算部', child: [{ part: '财务核算A部' }] }
      //     ]
      //   }
      // ],
      // departs: [
      //   {
      //     name: '总裁办',
      //     manager: '曹操',
      //     children: [{ name: '董事会', manager: '曹丕' }]
      //   },
      //   { name: '行政部', manager: '刘备' },
      //   { name: '人事部', manager: '孙权' }
      // ],
      // newprops: {
      //   label: 'part',
      //   children: 'child'
      // }
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '',
        manager: '负责人',
        children: []
      },
      departs: []
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {

    async getDepartmentList() {
      this.isLoading = true
      const { data } = await getDepartmentsListApi()
      // console.log(data)
      this.company.name = data.companyName
      this.departList = data.depts
      this.departs = transListToTreeData(data.depts, '')
      this.company.children = this.departs
      // console.log(this.departs)
      // console.log(this.departs[5].name)
      // console.log(this.departs[6].manager)
      this.isLoading = false
    },
    // transListToTreeData(list) {
    //   let arr = []
    //   arr = list.filter(item => item.pid === '')
    //   arr.forEach(item => {
    //     const children = list.filter(child => item.id === child.pid)
    //     item.children = children
    //   })
    // }
    // transListToTreeData(list, searchVal) {
    //   const arr = []
    //   list.forEach(item => {
    //     if (item.pid === searchVal) {
    //       const children = this.transListToTreeData(list, item.id)
    //       item.children = children
    //       arr.push(item)
    //     }
    //   })
    //   return arr
    // },
    handleClick(cmd, data) {
      // console.log(cmd)
      // console.log(data)
      if (cmd === 'add') {
        // console.log('add')
        this.addDepts(data)
        // console.log('点击的是添加')
      } if (cmd === 'edit') {
        this.editDepts(data)
        // console.log('edit')
      } if (cmd === 'del') {
        // console.log('del')
        this.delDepts(data.id)
      }
    },
    delDepts(id) {
      // console.log(id)
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        // 删除操作成功
        await delDepartmentsApi(id)
        // 添加提示
        this.$message.success('删除操作成功')
        this.getDepartmentList()
      }).catch(() => {})
    },
    addDepts(data) {
      this.showDialog = true
      this.nodeData = data
      // console.log(this.nodeData)
      // console.log(this.$refs.addDepts)
      // this.$refs.addDepts.getUserSimpleList()
    },
    editDepts(data) {
      this.showDialog = true
      this.nodeData = data
      this.$nextTick(() => {
        // 说明 异步dom更新完毕了
        this.$refs.addDepts.getDepartmentDetail()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  // ：：样式穿透
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    // 一个冒号和两个冒号一样效果。一个冒号是css2两个冒号是css3
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
